package com.gz.logistics.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gz.logistics.R;
import com.gz.logistics.app.Define;
import com.gz.logistics.library.util.CrashHandler;


public class FooterView extends LinearLayout implements OnClickListener, OnScrollListener {
	
	private static final String TAG = FooterView.class.getSimpleName();
	private boolean mIsRefreshing;
	private OnFooterViewRefreshListener mOnFooterViewRefreshListener;
	private ProgressBar mProgress;
	private TextView mLoadMore;

	public FooterView(Context context) {
		super(context);
	}

	public FooterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mProgress = (ProgressBar) findViewById(R.id.iv_progress);
		mLoadMore = (TextView) findViewById(R.id.bt_load);
		setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
//		if(mListener != null) {
//			mListener.onFooterViewClick();
//		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		switch (scrollState) {
		case OnScrollListener.SCROLL_STATE_FLING:
			// 当前正在执行滚动动画，并且手指不在屏幕上
			CrashHandler.d(TAG, "SCROLL_STATE_FLING");
			break;
		case OnScrollListener.SCROLL_STATE_IDLE:
			CrashHandler.d(TAG, "SCROLL_STATE_IDLE " + view.getLastVisiblePosition() + " " + (view.getCount() - 1));
			if(view.getLastVisiblePosition() == view.getCount() - 1){
				if(mOnFooterViewRefreshListener != null && !mIsRefreshing) {
					refresh();
					mOnFooterViewRefreshListener.onFooterViewRefresh();
				}
			}
			break;
		case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
			CrashHandler.d(TAG, "SCROLL_STATE_TOUCH_SCROLL");
			break;
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}
	
	public boolean isRefreshing() {
		return mIsRefreshing;
	}
	
	private void refresh(){
		mIsRefreshing = true;
		mLoadMore.setText(Define.MSG_UNDER_LOADING);
		mProgress.setVisibility(View.VISIBLE);
	}
	
	/**
	 * 刷新成功并且后面还有数据可以进行加载
	 */
	public void onRefreshSuccess(){
		mIsRefreshing = false;
		mProgress.setVisibility(View.GONE);
		mLoadMore.setText(Define.MSG_LOADING_MORE);
	}
	/**
	 * 刷新成功并且后面还有数据可以进行加载
	 */
	public void onRefreshSuccess(String str){
		mIsRefreshing = false;
		mProgress.setVisibility(View.GONE);
		mLoadMore.setText(str);
	}
	
	/**
	 * 刷新成功并且后面没有数据可以进行加载
	 */
	public void onRefreshComplete() {
		mIsRefreshing = false;
		mProgress.setVisibility(View.GONE);
		mLoadMore.setText(Define.MSG_NO_MORE_DATA);
	}
	
	/**
	 * 刷新失败
	 */
	public void onRefreshFailed(){
		mIsRefreshing = false;
		mProgress.setVisibility(View.GONE);
		mLoadMore.setText(Define.MSG_LOADING_FAILED_LOADING_AGEIN);
	
	}
	
	public void setOnFooterViewRefreshListener(OnFooterViewRefreshListener listener) {
		mOnFooterViewRefreshListener = listener;
	}
	
	public static interface OnFooterViewRefreshListener {
		void onFooterViewRefresh();
	}

	
}
